package com.example.icongyou.service;

import com.example.icongyou.api.TeamAssignmentApi;
import com.example.icongyou.bean.TeamAssignment;
import com.example.icongyou.retrofit.RetrofitClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import java.math.BigDecimal;
import java.util.List;

public class TeamAssignmentService {
    private TeamAssignmentApi api;

    public TeamAssignmentService() {
        api = RetrofitClient.getClient().create(TeamAssignmentApi.class);
    }

    // 创建团队作业
    public void createTeamAssignment(TeamAssignment teamAssignment, ApiCallback<TeamAssignment> callback) {
        api.createTeamAssignment(teamAssignment).enqueue(createCallback(callback));
    }

    // 根据ID获取团队作业
    public void getTeamAssignment(Long id, ApiCallback<TeamAssignment> callback) {
        api.getTeamAssignment(id).enqueue(createCallback(callback));
    }

    // 根据团队ID获取团队作业列表
    public void getTeamAssignmentsByTeam(Long teamId, ApiCallback<List<TeamAssignment>> callback) {
        api.getTeamAssignmentsByTeam(teamId).enqueue(createCallback(callback));
    }

    // 根据作业ID获取团队作业列表
    public void getTeamAssignmentsByAssignment(String assignmentId, ApiCallback<List<TeamAssignment>> callback) {
        api.getTeamAssignmentsByAssignment(assignmentId).enqueue(createCallback(callback));
    }

    // 根据提交者获取团队作业列表
    public void getTeamAssignmentsBySubmitter(Long submittedBy, ApiCallback<List<TeamAssignment>> callback) {
        api.getTeamAssignmentsBySubmitter(submittedBy).enqueue(createCallback(callback));
    }

    // 获取所有团队作业
    public void getAllTeamAssignments(ApiCallback<List<TeamAssignment>> callback) {
        api.getAllTeamAssignments().enqueue(createCallback(callback));
    }

    // 更新团队作业
    public void updateTeamAssignment(Long id, TeamAssignment teamAssignment, ApiCallback<TeamAssignment> callback) {
        api.updateTeamAssignment(id, teamAssignment).enqueue(createCallback(callback));
    }

    // 更新分数和优秀状态
    public void updateScore(Long id, BigDecimal score, Integer isExcellent, ApiCallback<Void> callback) {
        api.updateScore(id, score, isExcellent).enqueue(createCallback(callback));
    }

    // 提交作业
    public void submitAssignment(Long id, String filePath, ApiCallback<Void> callback) {
        api.submitAssignment(id, filePath).enqueue(createCallback(callback));
    }

    // 删除团队作业
    public void deleteTeamAssignment(Long id, ApiCallback<Void> callback) {
        api.deleteTeamAssignment(id).enqueue(createCallback(callback));
    }

    // 获取优秀作业列表
    public void getExcellentAssignments(ApiCallback<List<TeamAssignment>> callback) {
        api.getExcellentAssignments().enqueue(createCallback(callback));
    }

    // 根据团队ID和作业ID获取作业
    public void getByTeamAndAssignment(Long teamId, String assignmentId, ApiCallback<TeamAssignment> callback) {
        api.getByTeamAndAssignment(teamId, assignmentId).enqueue(createCallback(callback));
    }

    // 统计团队作业数量
    public void countByTeam(Long teamId, ApiCallback<Integer> callback) {
        api.countByTeam(teamId).enqueue(createCallback(callback));
    }

    // 统计作业提交数量
    public void countByAssignment(String assignmentId, ApiCallback<Integer> callback) {
        api.countByAssignment(assignmentId).enqueue(createCallback(callback));
    }

    // 通用的回调处理
    private <T> Callback<T> createCallback(ApiCallback<T> callback) {
        return new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                if (response.isSuccessful() && response.body() != null) {
                    callback.onSuccess(response.body());
                } else {
                    callback.onError("请求失败: " + response.code());
                }
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
                callback.onError("网络错误: " + t.getMessage());
            }
        };
    }

    // 回调接口
    public interface ApiCallback<T> {
        void onSuccess(T result);
        void onError(String error);
    }
}